<template>
  <view>
    <view class="search-uni">
      <uni-search-bar :radius="100" cancelButton="none" bgColor="#fff" placeholder="请输入搜索内容" @input="inputHandle"
        :focus="true"></uni-search-bar>
    </view>

    <!-- 搜索建议列表 -->
    <view class="search-suggest" v-if="searchResult.length !== 0">
      <view class="suggest-item" v-for="(item, i) in searchResult" :key="i" @click="toGoodsDetailPage(item)">
        <view class="goods-name">
          {{item.goods_name}}
        </view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>

    <!-- 搜索历史 -->
    <view class="search-history" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="cleanSearchHistory"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag :text="item" v-for="(item, i) in searchHistoryList" :key="i" @click="toGoodsListPage(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null,
        searchVal: '',
        // 搜索结果
        searchResult: [],
        searchHistoryList: []
      };
    },

    methods: {
      // 用户输入时
      inputHandle(e) {

        // 防抖
        clearTimeout(this.timer)

        this.timer = setTimeout(() => {
          this.searchVal = e.trim()
          this.getSearchResult()
        }, 500)
      },

      // 获取搜索结果
      async getSearchResult() {
        if (this.searchVal === '') {
          this.searchResult = []
          return
        }
        const {
          data: res
        } = await uni.$http.get("/api/public/v1/goods/qsearch", {
          query: this.searchVal
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        this.searchResult = res.message


      },

      // 跳转到商品详情页
      toGoodsDetailPage(goods) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
        });

        // this.searchVal = goods.goods_name
        this.saveSearchHistory()
      },

      // 存储历史搜索记录
      saveSearchHistory() {
        this.searchHistoryList = this.searchHistoryList.filter(item => item !== this.searchVal)
        this.searchHistoryList.unshift(this.searchVal)
        uni.setStorageSync('searchHistoryList', JSON.stringify(this.searchHistoryList))
      },

      // 清空搜索历史
      cleanSearchHistory() {
        this.searchHistoryList = [];
        uni.setStorageSync("searchHistoryList", "[]")
      },

      // 跳转到商品列表页
      toGoodsListPage(val) {
        uni.navigateTo({
          url: "/subpkg/goods_list/goods_list?query=" + val
        })
      },

    },

    onLoad() {
      this.searchHistoryList = JSON.parse(uni.getStorageSync("searchHistoryList") || "[]")
    }

  }
</script>

<style lang="scss">
  .search-uni {
    position: sticky;
    top: 0;
    z-index: 99;

    .uni-searchbar {
      background-color: #C00000;
    }
  }

  .search-suggest {
    padding: 0 5px;

    .suggest-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 13px 0;
      font-size: 12px;
      border-bottom: 1px solid #efefef;

      .goods-name {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }

  .search-history {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      height: 40px;
      align-items: center;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }

    .history-list {
      padding-top: 5px;

      .uni-tag {
        margin-right: 5px;
        background-color: #efefef;
        border: 0;
        color: #000;
      }
    }
  }
</style>